package cn.edu.cimv.service.impl;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.mapper.LoginMapper;
import cn.edu.cimv.pojo.Information;
import cn.edu.cimv.pojo.SystemProcs;
import cn.edu.cimv.service.LoginService;
import cn.edu.cimv.util.CryptionUtil;
import cn.edu.cimv.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by 门铎 on 2016/6/23.
 */
@Service
public class LoginServiceImpl implements LoginService{
    private static Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private Information information;
    @Override
    public Information checkIn(String username, String password) {

        if("".equals(password.trim())){
            information.setMessage(InfoConstant.LOGIN_FALSE);
            logger.error(username+"试图登录失败!时间：" + DateUtil.toSecondTimeStamp(new Date()));
            return information;
        }

        String pwd ="";

        try {
            //加密 验证
            pwd =  CryptionUtil.decryptByPrivateKey(loginMapper.checkIn(username));
        } catch (Exception e) {
            e.printStackTrace();
        }

        information.setDate(DateUtil.toDateString(new Date()));
        if (pwd.equals(password)){
            logger.info(username+"登录成功!时间：" + DateUtil.toSecondTimeStamp(new Date()));
            information.setMessage(InfoConstant.LOGIN_RIGHT);
            return information;
        }else{
            information.setMessage(InfoConstant.LOGIN_FALSE);
            logger.error(username+"试图登录失败!时间：" + DateUtil.toSecondTimeStamp(new Date()));
            return information;
        }
    }

    @Override
    public String getRoleId(String username) {

        return loginMapper.getRoleId(username);
    }

    @Override
    public String getUserId(String username) {
        return loginMapper.getUserId(username);
    }

    @Override
    public SystemProcs getSystemPraa() {
        SystemProcs system = new SystemProcs();
        system.setJdkVersion(System.getProperty("java.version"));
        system.setJavaHome(System.getProperty("java.home"));
        system.setOsName(System.getProperty("os.name"));
        system.setUserHome(System.getProperty("user.home"));
        system.setUserName(System.getProperty("user.name"));
        return system;
    }
}
